export class AudioManager {
    constructor() {
        this.musicVolume = 0.5;
        this.sfxVolume = 0.5;
        this.bgMusic = null;
        this.sounds = {};
        this.currentTheme = null;
        
        this.initializeAudio();
    }

    initializeAudio() {
        // 初始化音效
        this.sounds = {
            jump: new Audio('assets/audio/jump.wav'),
            collision: new Audio('assets/audio/collision.wav'),
            powerup: new Audio('assets/audio/powerup.wav'),
            score: new Audio('assets/audio/score.wav'),
            levelUp: new Audio('assets/audio/level_up.wav')
        };

        // 设置所有音效的音量
        Object.values(this.sounds).forEach(sound => {
            sound.volume = this.sfxVolume;
        });
    }

    loadBackgroundMusic(theme) {
        // 如果是相同主题，不需要重新加载
        if (theme === this.currentTheme && this.bgMusic) {
            this.playBackgroundMusic();
            return;
        }

        // 如果当前有音乐在播放，先淡出
        if (this.bgMusic && !this.bgMusic.paused) {
            this.fadeOutCurrentMusic(() => {
                this.switchBackgroundMusic(theme);
            });
        } else {
            this.switchBackgroundMusic(theme);
        }
    }

    switchBackgroundMusic(theme) {
        // 停止并清理旧的音乐
        if (this.bgMusic) {
            this.bgMusic.pause();
            this.bgMusic = null;
        }

        // 加载新的背景音乐
        this.currentTheme = theme;
        this.bgMusic = new Audio(`assets/audio/${theme}_bg.wav`);
        this.bgMusic.loop = true;
        this.bgMusic.volume = 0;  // 从0开始淡入
        
        // 播放新音乐并淡入
        this.bgMusic.play().then(() => {
            this.fadeInNewMusic();
        }).catch(() => {
            console.log('等待用户交互后播放背景音乐');
            // 存储音量设置，等待用户交互后恢复
            this.bgMusic.volume = this.musicVolume;
        });
    }

    fadeOutCurrentMusic(callback) {
        if (!this.bgMusic) return;
        
        const fadeInterval = setInterval(() => {
            if (this.bgMusic.volume > 0.1) {
                this.bgMusic.volume -= 0.1;
            } else {
                clearInterval(fadeInterval);
                this.bgMusic.pause();
                this.bgMusic.volume = this.musicVolume;
                if (callback) callback();
            }
        }, 100);
    }

    fadeInNewMusic() {
        if (!this.bgMusic) return;
        
        const fadeInterval = setInterval(() => {
            if (this.bgMusic.volume < this.musicVolume) {
                this.bgMusic.volume = Math.min(this.bgMusic.volume + 0.1, this.musicVolume);
            } else {
                clearInterval(fadeInterval);
            }
        }, 100);
    }

    playBackgroundMusic() {
        if (this.bgMusic) {
            this.bgMusic.volume = this.musicVolume;
            this.bgMusic.play().catch(() => {
                console.log('等待用户交互后播放背景音乐');
            });
        }
    }

    stopBackgroundMusic() {
        if (this.bgMusic) {
            this.fadeOutCurrentMusic();
        }
    }

    playSound(soundName) {
        const sound = this.sounds[soundName];
        if (sound) {
            // 克隆音频节点以支持重叠播放
            const soundClone = sound.cloneNode();
            soundClone.volume = this.sfxVolume;
            soundClone.play().catch(() => {
                console.log(`等待用户交互后播放音效: ${soundName}`);
            });
        }
    }

    setMusicVolume(volume) {
        this.musicVolume = volume;
        if (this.bgMusic) {
            this.bgMusic.volume = volume;
        }
    }

    setSFXVolume(volume) {
        this.sfxVolume = volume;
        Object.values(this.sounds).forEach(sound => {
            sound.volume = volume;
        });
    }

    pauseAll() {
        if (this.bgMusic) {
            this.bgMusic.pause();
        }
    }

    resumeAll() {
        if (this.bgMusic) {
            this.bgMusic.play().catch(() => {
                console.log('等待用户交互后恢复背景音乐');
            });
        }
    }

    // 切换主题时调用此方法
    changeTheme(theme) {
        if (theme !== this.currentTheme) {
            this.loadBackgroundMusic(theme);
        }
    }
} 